<?php

namespace App\Http\Services\FormBuilder\Elements;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

class Select extends FormControl
{
    private $options;
    private $selected;

    public $fillable = [
        'id','type','class', 'value','name'
        ];

    protected $attributes = [
        'class' => 'js-select2 form-control',
    ];

    public function detailBlock()
    {
        $class = $this->getAttribute('li_class');
        $displayName = $this->getAttribute('display_name');
        $value = $this->getAttribute('value');
        if (is_array($value)) {
            $value = ltrim(implode('/ ',$value),'/ ');
        }
        //add-by-chengjian
        $dynamic_options = $this->getAttribute('dynamic_options');
        if($dynamic_options) {
            $options  = $dynamic_options();
            $value = $options[$value];
        }
        $html = '';
        $html .= '<div class="items '.$class.'">';
        $html .= '<label class="'.$class.'">'.$displayName.'</label>';
        $html .= '<div class="detail '.$class.'">'.$value.'</div>';
        $html .='</div>';

        return $html;
    }

    public function select($option)
    {
        $this->selected = $option;

        return $this;
    }

    protected function setOptions($options)
    {
        $this->options = $options;
    }

    public function options($options)
    {
        $this->setOptions($options);

        return $this;
    }

    public function render()
    {
        $result = '<select';
        $result .= $this->renderAttributes();
        $result .= '>';
        $result .= $this->renderOptions();
        $result .= '</select>';

        return $result;
    }

    protected function renderOptions()
    {
        $result = '';

        if ($this->getAttribute('dynamic_options')) {
            $this->setOptions($this->getAttribute('dynamic_options')());
        }

        foreach ($this->options as $value => $label) {
            if (is_array($label)) {
                $result .= $this->renderOptGroup($value, $label);
                continue;
            }
            $result .= $this->renderOptionss($value, $label);
        }

        return $result;
    }

    protected function renderOptGroup($label, $options)
    {

        $result = "<optgroup label=\"{$label}\">";
        foreach ($options as $value => $label) {
            $result .= $this->renderOption($value, $label);
        }
        $result .= "</optgroup>";

        return $result;
    }

    protected function renderOptionss($value, $label)
    {
        $option = '<option ';
        $option .= 'value="' . $value . '"';
        $option .= $this->isSelected($value) ? ' selected' : '';
        $option .= '>';
        $option .= $label;
        $option .= '</option>';

        return $option;
    }

    protected function isSelected($value)
    {
        if ($value == $this->getAttribute('value')) {
            return true;
        } else {
            return false;
        }

        //return in_array($value, (array)$this->selected);
    }

    public function addOption($value, $label)
    {
        $this->options[$value] = $label;

        return $this;
    }

    public function prependOption($value, $label)
    {
        $this->options = Arr::prepend($this->options, $label, $value);

        return $this;
    }

    public function appendOption($value, $label)
    {
        $this->options = Arr::add($this->options, $value, $label);

        return $this;
    }

    public function placeholder($label = '-- Select --')
    {
        return $this->prependOption('', $label);
    }

    public function defaultValue($value)
    {
        if ($this->selected !== null) {
            return $this;
        }

        $this->select($value);

        return $this;
    }

    public function multiple()
    {
        $name = $this->attributes['name'];
        if (substr($name, -2) != '[]') {
            $name .= '[]';
        }

        $this->setName($name);
        $this->setAttribute('multiple', 'multiple');

        return $this;
    }


}
